/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		pub_priv.cpp
 * 位 置:
 *		src/biz/pub_priv.cpp
 * 描 述:
 *		系统权限类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "occi.h"
#include "pub/def.h"
#include "pub/db.h"
#include "pub/err.h"
#include "pub/str.h"
#include "pub/log.h"
#include "biz/pub_priv.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//  构造函数
//----------------------------------------------
PubPriv::PubPriv(Connection* conn)
{
	this->conn = conn;
}

//----------------------------------------------
//  析构函数
//----------------------------------------------
PubPriv::~PubPriv()
{
	this->conn = 0;
}

//----------------------------------------------
//	检查系统状态
//----------------------------------------------
void PubPriv::checkSys()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("检查系统状态是否允许执行...");
		stmt->setSQL(" \
			select count(*) \
			from ConfSvcSys \
			where serviceCode = :serviceCode \
			and :sysStatus in sysStatus");
		stmt->setString(1, serviceCode);
		stmt->setString(2, sysStatus);
		rs = stmt->executeQuery();
		rs->next();
		int count = rs->getInt(1);
		if (count <= 0)
		{
			throw AppException("当前系统状态不允许执行");
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("当前系统状态允许执行.");
	}
	catch (exception& e)
	{
		Log::error("检查系统状态出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  检查交易帐号状态
//----------------------------------------------
void PubPriv::checkAcctTrans()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("检查交易帐号状态是否允许执行...");
		stmt->setSQL(" \
			select count(*) \
			from ConfSvcAcct \
			where serviceCode = :serviceCode \
			and acctType = '0' \
			and :transAcctStatus in acctStatus");
		stmt->setString(1, serviceCode);
		stmt->setString(2, transAcctStatus);
		rs = stmt->executeQuery();
		rs->next();
		int count = rs->getInt(1);
		if (count <= 0)
		{
			throw AppException("当前交易账户状态不允许执行");
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("当前交易帐号状态允许执行.");
	}
	catch (exception& e)
	{
		Log::error("检查交易帐号状态出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  检查基金帐号状态
//----------------------------------------------
void PubPriv::checkAcctTA()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("检查基金帐号状态是否允许执行...");
		stmt->setSQL(" \
			select count(*) \
			from ConfSvcAcct \
			where serviceCode = :serviceCode \
			and acctType = '1' \
			and :taAcctStatus in acctStatus");
		stmt->setString(1, serviceCode);
		stmt->setString(2, taAcctStatus);
		rs = stmt->executeQuery();
		rs->next();
		int count = rs->getInt(1);
		if (count <= 0)
		{
			throw AppException("当前基金账户状态不允许执行");
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("当前基金帐号状态允许执行.");
	}
	catch (exception& e)
	{
		Log::error("检查基金帐号状态出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  检查基金状态
//----------------------------------------------
void PubPriv::checkFund()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("检查基金状态是否允许执行...");
		stmt->setSQL(" \
			select count(*) \
			from ConfSvcFund \
			where serviceCode = :serviceCode \
			and :fundStatus in fundStatus");
		stmt->setString(1, serviceCode);
		stmt->setString(2, fundStatus);
		rs = stmt->executeQuery();
		rs->next();
		int count = rs->getInt(1);
		if (count <= 0)
		{
			throw AppException("当前基金状态不允许执行");
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("当前基金状态允许执行.");
	}
	catch (exception& e)
	{
		Log::error("检查基金状态出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}
